layui.config({
	base: '../../js/',
})

var treeParam;

layui.use(['treeTable','layer','code','form'],function(){
	var form = layui.form,
		layer = layui.layer,
		treeTable = layui.treeTable;
	treeParam = {
		type: 'post',
		elem: '#tree-table',
		url: access_url + '/system/systemmanage/queryorglist?',
		icon_key: 'orgname',
		primary_key : 'orgid',
		parent_key : 'parent_id',
		is_checkbox: false,
		end: function(e){
			form.render();
		},
		cols: [
			{
				key: 'orgname',
				title: '组织机构名称',
				width: '200px'
			},
			{
				key: 'orgid',
				title: '组织机构编码',
				width: '200px'
			},
			{
				key: 'orgTypeDesc',
				title: '组织机构类型',
				width: '200px'
			},
			{
				key: 'remark',
				title: '备注',
				template: function(item){
					return item.remark==null?"":item.remark;
				},
				width: '500px'
			},
			{
				title: '操作',
				align: 'center',
				template: function(item){
					return `<a class="layui-btn layui-btn-xs" lay-filter="update">编辑</a>
							<a class="layui-btn layui-btn-danger layui-btn-xs" lay-filter="delete">删除</a>
							<a class="layui-btn layui-btn-normal layui-btn-xs" lay-filter="add">新增下级</a>`;
				},
				width: '300px'
			}
		]
	};
	
	var re = treeTable.render(treeParam);
	
	// 监听自定义
	treeTable.on('tree(update)',function(data){
		console.log("data",data);
		var orgInfo = data.item;
		
		layer.open({
			type : 2
			,title : '修改组织机构'
			,area : ['700px','550px']
			,offset : '20px'
			,fixed : false
			,content : 'updateOrg.html'
			,resize : false
			,success: function (layero, index) {
	            // 获取子页面的iframe
	            var iframe = window['layui-layer-iframe' + index];
	            // 向子页面的全局函数child传参
	            iframe.transmitParam(orgInfo);
	        }
		})
	})
	
	treeTable.on('tree(delete)',function(data){
		console.log("data",data);
		var orgid = data.item.orgid;
		var orgname = data.item.orgname;
		var org_type = data.item.org_type;
		
		layer.confirm('确定删除该机构以及下属子机构?',{icon:3,title:'提示'},function(index){
			layer.load(2);
			$.ajax({
				type : 'post'
			    ,dataType : 'json'
			    ,url : access_url+'/system/systemmanage/deleteorg'
			    ,data : {
			    	orgid : orgid
			    	,orgname : orgname
			    	,org_type : org_type
			    }
				,success : function(data){
					layer.closeAll('loading');
					if(data.code != 200){
						layer.msg(data.msg, {icon: 5});
						return;
					}
					layer.alert(data.msg,{icon:6});
					loadTable();
					layer.close(index);
				}
			})
		})
	})
	
	treeTable.on('tree(add)',function(data){
		console.log("data",data);
		var orgid = data.item.orgid;
		var orgname = data.item.orgname;
		var org_type = data.item.org_type;
		
		var pOrgJson = {
				orgid : orgid,
				orgname : orgname
		}
		
		layer.open({
			type : 2
			,title : '添加组织机构'
			,area : ['700px','550px']
			,offset : '20px'
			,fixed : false
			,content : 'addOrg.html'
			,resize : false
			,success: function (layero, index) {
	            // 获取子页面的iframe
	            var iframe = window['layui-layer-iframe' + index];
	            // 向子页面的全局函数child传参
	            iframe.transmitParam(pOrgJson);
	        }
		})
	})
	
	// 刷新重载树表（一般在异步处理数据后刷新显示）
	$('#queryOrg').click(function(){
		loadTable();
	})
	// 全部展开
	$('.open-all').click(function(){
		treeTable.openAll(re);
	})
	// 全部关闭
	$('.close-all').click(function(){
		treeTable.closeAll(re);
	})
})

function loadTable(){
	layui.use(['treeTable'],function(){
		var treeTable = layui.treeTable;
		var orgname = $('#orgname').val();
		var orgid = $('#orgid').val();
		var requesturl = treeParam.url;
		var url = requesturl.substring(0,requesturl.indexOf('?')+1);
		
		var param = '';
		
		if(!isNull(orgname)){
			param += '&orgname='+orgname;
		}
		if(!isNull(orgid)){
			param += '&orgid='+orgid;
		}
		
		if(!isNull(param)){
			param = param.substring(1,param.length);
			treeParam.url = url + param;
		} else {
			treeParam.url = url;
		}
		
		treeTable.render(treeParam);
	})
}


$('#add-org').click(function(){
	layer.open({
		type : 2
		,title : '添加组织机构'
		,area : ['700px','550px']
		,offset : '20px'
		,fixed : false
		,content : 'addOrg.html'
		,resize : false
	})
})